package com.imay.live.view.dialog;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.imay.base.fragment.BaseDialogFragment;
import com.imay.live.R;

/**
 * Created by jeaman on 2017/4/18.
 */

public class LoadingDialogFragment extends BaseDialogFragment {
    private final static String BUNDLE_NAME_MESSAGE = "message";
    private final static String BUNDLE_NAME_CANCELABLE = "cancelable";
    private ImageView mImgView;

    public static LoadingDialogFragment newInstance(String message, boolean cancelable) {
        Bundle bundle = new Bundle();
        bundle.putString(BUNDLE_NAME_MESSAGE, message);
        bundle.putBoolean(BUNDLE_NAME_CANCELABLE, cancelable);
        LoadingDialogFragment fragment = new LoadingDialogFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_loading;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        boolean cancelable = getArguments().getBoolean(BUNDLE_NAME_CANCELABLE);
        if (!cancelable) {
            setCancelable(cancelable);
        }
        String message = getArguments().getString(BUNDLE_NAME_MESSAGE);
        mImgView = $(R.id.dialog_loading_img);
        TextView textView = $(R.id.dialog_loading_tv_message);
        if (message != null) {
            textView.setText(message);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        ((AnimationDrawable) mImgView.getDrawable()).start();
    }

    @Override
    public void onPause() {
        super.onPause();
        ((AnimationDrawable) mImgView.getDrawable()).stop();
    }
}
